package com.gexiaobao.pigeon.ui.adapter

import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cxd.chatview.moudle.ChatView
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.model.UIMessageEntity
import com.gexiaobao.pigeon.app.util.ImageLoaderManager
import com.gexiaobao.pigeon.app.util.ImageLoaderManager.loadRoundImage
import com.gexiaobao.pigeon.app.util.Util
import com.xinbida.wukongim.message.type.WKSendMsgResult
import org.json.JSONException
import org.json.JSONObject


class ChatLiveMessageAdapter : BaseMultiItemQuickAdapter<UIMessageEntity, BaseViewHolder>() {
    init {
        addItemType(1, R.layout.send_item_msg_live_layout)
        addItemType(0, R.layout.recv_item_msg_live_layout)
    }

    override fun convert(baseViewHolder: BaseViewHolder, uiMessageEntity: UIMessageEntity) {
        val contentTv = baseViewHolder.getView<TextView>(R.id.contentTv)
        val contentIv = baseViewHolder.getView<ImageView>(R.id.contentIv)
        val nameTv = baseViewHolder.getView<TextView>(R.id.nameIv)
        if (uiMessageEntity.itemType == 1) { //发送消息
            val rlContentIv = baseViewHolder.getView<RelativeLayout>(R.id.rlContentIv)
            val statusIV = baseViewHolder.getView<ImageView>(R.id.statusTv)
            val statusTvPic = baseViewHolder.getView<ImageView>(R.id.statusTvPic)
            val llSendContentText = baseViewHolder.getView<LinearLayout>(R.id.llSendContentText)
            if (uiMessageEntity.msg.baseContentMsgModel.type == 1) { //文字
                llSendContentText.visibility = View.VISIBLE
                rlContentIv.visibility = View.GONE
                when (uiMessageEntity.msg.status) {
                    WKSendMsgResult.send_success -> {
                        statusIV.setImageResource(R.mipmap.success)
                        statusTvPic.setImageResource(R.mipmap.success)
                    }

                    WKSendMsgResult.send_loading -> {
                        statusIV.setImageResource(R.mipmap.loading)
                        statusTvPic.setImageResource(R.mipmap.loading)
                    }

                    else -> {
                        statusIV.setImageResource(R.mipmap.icon_error)
                        statusTvPic.setImageResource(R.mipmap.icon_error)
                    }
                }
            } else {
                llSendContentText.visibility = View.GONE
                rlContentIv.visibility = View.VISIBLE
            }
        } else { //接收消息
            val ivHeadReceiveMsg = baseViewHolder.getView<AppCompatImageView>(R.id.ivHeadReceiveMsg)
            if (uiMessageEntity.msg.memberOfFrom != null) {
                val head = uiMessageEntity.msg.memberOfFrom.memberAvatar
                if (head != null) {
                    ImageLoaderManager.loadCircleImageFall(context, head, ivHeadReceiveMsg, R.mipmap.profilepic_str_1)
                }
            }
            val chatViewRecvMsg = baseViewHolder.getView<ChatView>(R.id.chatViewRecvMsg)
            if (uiMessageEntity.msg.baseContentMsgModel.type == 1) { //文字
                chatViewRecvMsg.visibility = View.VISIBLE
                contentIv.visibility = View.GONE
            } else {
                chatViewRecvMsg.visibility = View.GONE
                contentIv.visibility = View.VISIBLE
            }
        }
        val mFromUID = uiMessageEntity.msg.fromUID
        if (mFromUID.length > 1) {
            val name = uiMessageEntity.msg.fromUID.substring(0, 1)
            nameTv.text = name
        } else {
            nameTv.text = mFromUID
        }
        if (uiMessageEntity.msg.baseContentMsgModel != null) {
            if (uiMessageEntity.msg.baseContentMsgModel.type == 1) { //文字
                contentTv.text = uiMessageEntity.msg.baseContentMsgModel.getDisplayContent()
            } else {
                val content = uiMessageEntity.msg.content
                if (content.isNotEmpty()) {
                    try {
                        val jsonObject = JSONObject(content)
                        var url = ""
                        var width = 0
                        var height = 0
                        url = if (jsonObject.has("url")) {
                            jsonObject.getString("url")
                        } else {
                            ""
                        }
                        //                        ImageLoaderManager.INSTANCE.loadRoundImage(getContext(), url, contentIv, 12);
                        if (jsonObject.has("width")) {
                            width = jsonObject.getInt("width")
                        }
                        if (jsonObject.has("height")) {
                            height = jsonObject.getInt("height")
                        }
                        setIMG(width, height, contentIv, url)
                    } catch (e: JSONException) {
                        throw RuntimeException(e)
                    }
                }
            }
        }
    }

    private fun setIMG(width: Int, height: Int, ivPic: ImageView, url: String) {
        var width = width
        var height = height
        val layoutParams = ivPic.layoutParams
        val defaultWith = Util.dp2px(context, 140f)
        val defaultHeight = Util.dip2px(context, 140f)
        var rate = 0f
        if (0 < width && 0 < height) {
            rate = width / java.lang.Float.valueOf(height.toFloat())
        }
        if (rate == 1f || rate == 0f) {
            ivPic.maxWidth = defaultWith
            ivPic.maxHeight = defaultHeight
            layoutParams.width = defaultWith
            layoutParams.height = defaultHeight
        } else if (rate > 1) {
            width = defaultWith
            ivPic.maxWidth = defaultWith
            rate = 3f.coerceAtMost(rate)
            ivPic.maxHeight = (width / rate).toInt()
            layoutParams.width = defaultWith
            layoutParams.height = (width / rate).toInt()
        } else {
            height = defaultHeight
            rate = (1 / 3.0f).coerceAtLeast(rate)
            ivPic.maxWidth = (height * rate).toInt()
            ivPic.maxHeight = defaultHeight
            layoutParams.width = (height * rate).toInt()
            layoutParams.height = defaultHeight
        }
        loadRoundImage(context, url, ivPic, 12)
    }
}
